package engine.systems.render.tv
{
	import ash.core.Component;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	
	public class TV extends Component
	{
		internal var _invalidate:Boolean = false;
		
		public var screen:Rectangle;
		public var fit:Boolean = true;
		
		private var _data:BitmapData = new BitmapData(1, 1, true, 0);
		public var x:Number;
		public var y:Number;
		
		public var display:DisplayObject;
		
		public function TV(display:DisplayObject, fit:Boolean = true, x:Number = 0, y:Number = 0, width:int = 1, height:int = 1)
		{
			this.fit		= fit;
			this.display 	= display;
			this.x 			= x;
			this.y			= y;
			
			this.resize(width, height);
		}
		
		override public function destroy():void
		{
			_data.dispose();
			_data = null;
			
			display = null;
		}
		
		public function get data():BitmapData { return _data; }
		
		public function resize(width:int, height:int):void
		{
			if(width < 1) 	width = 1;
			if(height < 1) 	height = 1;
			
			if(_data.width == width && _data.height == height) return;
			
			this.recalculate(width, height);
		}
		
		public function get width():int { return _data.width; }
		public function set width(width:int):void
		{
			if(width < 1) width = 1;
			
			if(width == _data.width) return;
			
			this.recalculate(width, _data.height);
		}
		
		public function get height():int { return _data.height; }
		public function set height(height:int):void
		{
			if(height < 1) height = 1;
			
			if(height == _data.height) return;
			
			this.recalculate(_data.width, height);
		}
		
		private function recalculate(width:int, height:int):void
		{
			_data.dispose();
			_data = null;
			_data = new BitmapData(width, height, true, 0);
			
			this.invalidate = true;
		}
	}
}